using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Serious
{
    public class Cube
    {
        public Vector3 origin;
        public Vector3 half_size;
        public Single angle; // degrees

        public Vector3 offset_from_center;

        public Vector3 velocity;
        public Single angle_delta;

        public string color_name;
        public BasicModel model
        {
            get
            {
                //return BasicModel.ColoredCubes[color_name];
                return BasicModel.UnitCubeTextured;
            }
        }
        
        public Rectangle3 bounds
        {
            get
            {
                return new Rectangle3((origin-offset_from_center)-half_size, half_size*2.0f);
            }
            set
            {
                origin = value.Mid + offset_from_center;
                half_size = value.size / 2.0f;
            }
        }
        public Matrix transformation
        {
            get
            {
                return Matrix.CreateTranslation(-offset_from_center / half_size) *
                       Matrix.CreateScale(half_size) *
                       Matrix.CreateRotationZ(MathHelper.ToRadians(angle)) *
                       Matrix.CreateTranslation(origin);
            }
        }

        public Cube (Vector3 center, Vector3 half_size, Single angle, Vector3 offset_from_center, Vector3 velocity, Single angle_delta, string color_name)
        {
            this.origin = center;
            this.half_size = half_size;
            this.angle = angle;

            this.offset_from_center = offset_from_center;

            this.velocity = velocity;
            this.angle_delta = angle_delta;

            this.color_name = color_name;
        }
        public Cube cloned ()
        {
            return new Cube(origin, half_size, angle, offset_from_center, velocity, angle_delta, color_name);
        }
        public void set (Cube other)
        {
            this.origin = other.origin;
            this.half_size = other.half_size;
            this.angle = other.angle;

            this.offset_from_center = other.offset_from_center;

            this.velocity = other.velocity;
            this.angle_delta = other.angle_delta;

            this.color_name = other.color_name;
        }

        public void Update (GameTime gameTime)
        {
            origin += velocity;
            angle += angle_delta;
            angle = MathExt.WrapAngleDeg(angle);
        }
        
        public void Draw (GameTime gameTime, GraphicsDevice device)
        {
            model.Draw(device);
        }
    }
}
